package com.pzhu.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pzhu.mapper.MenuMapper;
import com.pzhu.model.entity.Menu;
import com.pzhu.model.vo.MenuVo;
import com.pzhu.service.MenuService;
import com.pzhu.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author : zqh
 * @Date : 2023/11/26 0026 下午 4:17
 * @Version: 1.0
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    /**
     * 构建菜单树
     * @param menuList
     * @return
     */
    @Override
    public List<MenuVo> buildTreeMenu(List<Menu> menuList) {
        List<MenuVo> resultMenuList = new ArrayList<>();

        List<MenuVo> menuVoList = BeanCopyUtils.copyBeanList(menuList, MenuVo.class);
        for (MenuVo menuVo : menuVoList) {
            //寻找子节点
            for (MenuVo e : menuVoList) {
                if (e.getParentId().equals(menuVo.getId())) {
                    menuVo.getChildren().add(e);
                }
            }
            //判断父节点，添加到集合
            if (menuVo.getParentId() == 0L) {
                resultMenuList.add(menuVo);
            }
        }
        return resultMenuList;
    }
}
